package cn.com.bluemoon.daps.sync.config;

import cn.com.bluemoon.daps.common.toolkit.BmAssetUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * redisson
 *
 * @author Jarod.Kong
 */
@Configuration
public class RedissonConfig {
    @Value(value = "${spring.redis.cluster.nodes}")
    private String redissonAddress;

    @Value(value = "${spring.redis.cluster.password}")
    private String password;

    @Value(value = "${spring.redis.cluster.database:0}")
    private Integer database;

    @Bean
    public RedissonClient redissonClient() {
        String[] nodes = redissonAddress.split(",");
        for (int i = 0; i < nodes.length; i++) {
            nodes[i] = "redis://" + nodes[i];
        }
        BmAssetUtils.isTrue(nodes.length > 0, "请配置redis信息");

        Config config = new Config();
        if (nodes.length == 1) {
            config.useSingleServer().setPassword(password).setAddress(nodes[0]).setDatabase(database);
        } else {
            config.useClusterServers().setPassword(password).addNodeAddress(nodes);
        }
        return Redisson.create(config);
    }

}
